/*
 * EnigmaGM - A feature rich guild management system.
 * Copyright (C) 2007 ajiaojr.
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */
package org.ajiaojr.enigmagm.client;

import org.ajiaojr.enigmagm.client.data.User;
import org.ajiaojr.enigmagm.client.event.UserEventListener;
import org.ajiaojr.enigmagm.client.event.UserEventListenerCollection;
import org.ajiaojr.enigmagm.client.service.UserService;
import org.ajiaojr.enigmagm.client.service.UserServiceAsync;

import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.Iterator;

/**
 * @author ajiaojr
 */
public class UserSessionManager {
  private static UserSessionManager instance = new UserSessionManager();

  /**
   * @return
   */
  public static UserSessionManager getInstance() {
    return instance;
  }

  private User user;

  private UserEventListenerCollection userEventListeners =
      new UserEventListenerCollection();

  private UserServiceAsync userService = UserService.Util.getInstance();

  private UserSessionManager() {
  }

  /**
   * @param listener
   */
  public void addUserEventListener(UserEventListener listener) {
    userEventListeners.add(listener);
  }

  /**
   * @return
   */
  public User getUser() {
    return user;
  }

  /**
   * @param username
   * @param password
   * @param saveLogin
   */
  public void login(String username, String password, boolean saveLogin) {
    userService.login(username, password, saveLogin, new AsyncCallback() {

      public void onFailure(Throwable caught) {
        userEventListeners.fireLoginFailureEvent(caught);
      }

      public void onSuccess(Object result) {
        user = (User) result;
        userEventListeners.fireLoginSuccessEvent();
      }
    });
  }

  public void loginViaCookie() {
    // Try to get the username and auth token.
    String username = Cookies.getCookie("USERNAME");
    String authToken = Cookies.getCookie("AUTH_TOKEN");

    // If the auth token exists
    if (username != null && authToken != null) {
      userService.loginViaAuthToken(new AsyncCallback() {

        public void onFailure(Throwable caught) {
          userEventListeners.fireLoginFailureEvent(caught);
        }

        public void onSuccess(Object result) {
          user = (User) result;
          userEventListeners.fireLoginSuccessEvent();
        }
      });
    }
  }

  /**
   * 
   */
  public void logout() {
    userService.logout(new AsyncCallback() {

      public void onFailure(Throwable caught) {
        userEventListeners.fireLogoutFailureEvent(caught);
      }

      public void onSuccess(Object result) {
        user = null;
        userEventListeners.fireLogoutSuccessEvent();
      }
    });
  }

  /**
   * @param username
   * @param password
   */
  public void register(String username, String password) {
    userService.register(username, password, new AsyncCallback() {

      public void onFailure(Throwable caught) {
        Iterator it = userEventListeners.iterator();
        while (it.hasNext()) {
          UserEventListener listener = (UserEventListener) it.next();
          listener.onRegistrationFailure(caught);
        }
      }

      public void onSuccess(Object result) {
        Iterator it = userEventListeners.iterator();
        while (it.hasNext()) {
          UserEventListener listener = (UserEventListener) it.next();
          listener.onRegistrationSuccess();
        }
      }
    });
  }

  /**
   * @param listener
   */
  public void removeUserEventListener(UserEventListener listener) {
    userEventListeners.remove(listener);
  }
}
